﻿using System.Web;

namespace Project.Tool
{
    public static class SessionHelper
    {
        /// <summary>
          /// 添加Session，调动有效期为20分钟
          /// </summary>
          /// <param name="strSessionName">Session对象名称</param>
          /// <param name="strValue">Session值</param>
        public static void Add(string strSessionName, object strValue)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = 20;
        }

        /// <summary>
          /// 修改Session
          /// </summary>
          /// <param name="strSessionName">Session对象名称</param>
          /// <param name="strValue">Session值</param>
        public static void Update(string strSessionName, object strValue)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
        }

        /// <summary>
          /// 添加Session
          /// </summary>
          /// <param name="strSessionName">Session对象名称</param>
          /// <param name="strValue">Session值</param>
          /// <param name="iExpires">调动有效期（分钟）</param>
        public static void Add(string strSessionName, object strValue, int iExpires)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = iExpires;
        }

        /// <summary>
          /// 读取某个Session对象值
          /// </summary>
          /// <param name="strSessionName">Session对象名称</param>
          /// <returns>Session对象值</returns>
        public static T Get<T>(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName].Equals(null))
            {
                return default(T);
            }
            else
            {
                return (T)HttpContext.Current.Session[strSessionName];
            }
        }

        /// <summary>
          /// 删除某个Session对象
          /// </summary>
          /// <param name="strSessionName">Session对象名称</param>
        public static void Delete(string strSessionName)
        {
            HttpContext.Current.Session[strSessionName] = null;
        }
    }
}
